<template>
    <div class="container">
        <!-- 顶部导航栏 -->
        <van-nav-bar
        title="搜索"
        left-text="返回"
        left-arrow
        @click-left="onClickLeft"/>
        <!-- 搜索框 -->
        <van-search
            v-model="value"
            show-action
            placeholder="请输入搜索关键词">
            <template #action>
                <div @click="getProduct">搜索</div>
            </template>
        </van-search>

        <!-- 没有数据展示页面 -->
        <div class="noPro" v-if="isHavProduct">
            暂时没有找到更多数据哦
        </div>

        <!-- 请求回数据展示页面 -->
        <div v-else class="showList">
            <van-tabs v-model="active">
                <!-- 循环遍历渲染数据 -->
                <van-tab title="个护清洁" v-if="!!data.clearData[0]" class="userLike">
                    <div class="main_container">
                        <div class="likeProduct" v-for="item in data.clearData" :key="item.id" @click="goDetails(item.type)">
                            <img :src="item.img" alt="">
                            <p class="name">{{item.name}}</p>
                            <span>￥{{item.price}}</span>
                            <p class="type">{{item.subType}}</p>
                        </div>
                    </div>
                </van-tab>
                <van-tab title="休闲食品" v-if="!!data.foodData[0]" class="userLike">
                    <div class="main_container">
                        <div class="likeProduct" v-for="item in data.foodData" :key="item.id" @click="goDetails(item.type)">
                            <img :src="item.img" alt="">
                            <p class="name">{{item.name}}</p>
                            <span>￥{{item.price}}</span>
                            <p class="type">{{item.subType}}</p>
                        </div>
                    </div>
                </van-tab>
                <van-tab title="医药保健" v-if="!!data.medicalData[0]" class="userLike">
                    <div class="main_container">
                        <div class="likeProduct" v-for="item in data.medicalData" :key="item.id" @click="goDetails(item.type)">
                            <img :src="item.img" alt="">
                            <p class="name">{{item.name}}</p>
                            <span>￥{{item.price}}</span>
                            <p class="type">{{item.subType}}</p>
                        </div>
                    </div>
                </van-tab>
            </van-tabs>
        </div>
    </div>
</template>

<script>
    import { searchApi } from '../../apis/searchApi'
    export default {
        data(){
            return {
                value:'', //搜索框的值
                data:[], //请求回来的数据
                isHavProduct: false, //是否请求回来商品
                active:0,
            }
        },
        created(){
            this.value = this.$route.query.value || ''
            this.$route.query.value = ''
            this.getProduct()
        },
        methods:{
            /**
             * 点击返回跳转回上一个页面
             */
            onClickLeft(){
                this.$router.go(-1)
            },
            /**
             * 获取商品数据
             */
            async getProduct(){
                let result = await searchApi(this.value)
                //如果请求回来code为2000
                if(result.code === 2000){
                    this.isHavProduct = true
                    return
                }
                this.data = result.data
                console.log(this.data);
                this.isHavProduct = false
            },
            //点击商品进入详情页面，按理来说应该是传递ID，但是这里是模拟数据，
            goDetails(type){
                this.$router.push('/details?type=' + type)
            }
        }
    }
</script>

<style lang="less" scoped>
    @import '../../style/common.less';
    
    .container{
       .loginCommon();
       .showList{
           height: calc(100% - 46px - 54px);
           width: 100%;
           margin: auto;
       }
    }

    //列表样式
    .userLike{
        position: relative;
        display: block;
        margin: auto;
        width: 90%;
        >p{
            text-align: center;
            font-size: .7rem;
        }
        .main_container{
            width: 100%;
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
            div{
                width: 47%;
                margin-bottom: 20px;
                background: #f5f5f5;
                border-radius: 10px;
                img{
                    width: 100%;
                    border-top-left-radius: 10px;
                    border-top-right-radius: 10px;
                }
                .name{
                    font-size: .8rem;
                    font-weight: 700;
                    display: -webkit-box;
                    text-overflow: ellipsis;
                    overflow: hidden;
                    -webkit-box-orient: vertical;
                    -webkit-line-clamp: 2;//当属性值为3，表示超出3行隐藏
                    margin: 0;
                }
                span{
                    color: #f40;
                    line-height: 20px;
                }
                .type{
                    margin: 5px;
                    font-size: .7rem;
                    color: gray;
                }
            }
        }
    }

</style>